package _15_XML与JSON._2_JSON;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;

import java.text.ParseException;
import java.util.*;

public class OrgJSONTest {

    @Test
    public void f4(){
        JSONStringVO jsonStringVO = new JSONStringVO();
        String s = jsonStringVO.jsonString4;
        JSONObject jsonObject = new JSONObject(s);
        jsonObject.put("name","jby");
        s= jsonObject.toString();
        System.out.println();
        String ss = jsonObject.get("name").toString();
        System.out.println(ss);
    }

    //获取对象——>对象——>值
    //获取数组——>对象——>值
    @Test
    public void f(){
        JSONStringVO jsonStringVO = new JSONStringVO();
        String s = jsonStringVO.jsonString3;
        JSONObject jsonObject = new JSONObject(s);
        System.out.println();

        JSONObject carQuoteQuotationDate_JSONObject = jsonObject.getJSONObject("carQuoteQuotationDate");
        System.out.println(carQuoteQuotationDate_JSONObject.getJSONObject("ciInsureDemandSubBI").get("applyNo"));

        JSONArray carQuoteTransProposalReqList_JSONArray = jsonObject.getJSONArray("carQuoteTransProposalReqList");

        System.out.println("aliasName：" + carQuoteTransProposalReqList_JSONArray.getJSONObject(0).get("aliasName"));
        System.out.println();
    }

    //获取 数组——>对象
    //获取 数组——>对象——>数组
    //获取 数组——>对象——>值
    @Test
    public void f2(){
//        StringBuffer stringBuffer = new StringBuffer();

        JSONStringVO jsonStringVO = new JSONStringVO();
        String s = jsonStringVO.jsonString2;

        JSONObject jsonObject = new JSONObject(s);
        //carQuoteTransProposalRspList
        //JSONArray
        JSONArray carQuoteTransProposalRspList_JSONArray = jsonObject.getJSONObject("data").getJSONArray("carQuoteTransProposalRspList");
        //数组个数
        int carQuoteTransProposalRspList_JSONArray_Length = carQuoteTransProposalRspList_JSONArray.length();
        //字段名
        String[] carQuoteTransProposalRspList_NameArray = {"quotationNo","proposalNo","sumPremium","sumPayTax","giftpackageId","prpCengageVoRspList","isMsgCheck","carQuoteCoinsInfoRsp","carQuotePayeeAccountRsp"};
        //字段名个数
        int carQuoteTransProposalRspList_NameArray_Length = carQuoteTransProposalRspList_NameArray.length;

        //prpCengageVoRspList
        //JSONArray
        JSONArray prpCengageVoRspList_JSONArray;
        //数组个数
        int prpCengageVoRspList_JSONArray_Length;
        //字段名
        String[] prpCengageVoRspList_NameArray = {"serialNo","clauseCode","clauseName","clauses","engageFlag","flag","engageType"};
        //字段名个数
        int prpCengageVoRspList_NameArray_Length = prpCengageVoRspList_NameArray.length;

        for(int i = 0; i < carQuoteTransProposalRspList_JSONArray_Length; i++){
            System.out.println("carQuoteTransProposalRspList，第" + (i + 1) + "条数据 === start");


            for(int j = 0; j < carQuoteTransProposalRspList_NameArray_Length; j++){

                if(carQuoteTransProposalRspList_NameArray[j].equals("prpCengageVoRspList")){
                    prpCengageVoRspList_JSONArray = carQuoteTransProposalRspList_JSONArray.getJSONObject(i).getJSONArray(carQuoteTransProposalRspList_NameArray[j]);
                    prpCengageVoRspList_JSONArray_Length = prpCengageVoRspList_JSONArray.length();

                    for(int x = 0; x < prpCengageVoRspList_JSONArray_Length; x++){
                        System.out.println("prpCengageVoRspList，第" + (x + 1) + "条数据 === start");
                        for(int y = 0; y < prpCengageVoRspList_NameArray_Length; y++){
                            System.out.println(prpCengageVoRspList_NameArray[y] + ": " + prpCengageVoRspList_JSONArray.getJSONObject(x).get(prpCengageVoRspList_NameArray[y]));
                        }
                        System.out.println("prpCengageVoRspList，第" + (x + 1) + "条数据 === end\n");
                    }
                }
                else{
                    System.out.println(carQuoteTransProposalRspList_NameArray[j] + ": " + carQuoteTransProposalRspList_JSONArray.getJSONObject(i).get(carQuoteTransProposalRspList_NameArray[j]));
                }
            }
            System.out.println("carQuoteTransProposalRspList，第" + (i + 1) + "条数据 === end\n");
        }




    }



    //对象——>数组——>对象
    @Test
    public void f5(){
        JSONStringVO jsonStringVO = new JSONStringVO();
        String s = jsonStringVO.jsonString5;
        JSONObject dataObject = new JSONObject(s);
//        System.out.println(dataObject.getJSONObject("data").getJSONArray("batchVehicleLists").getJSONObject(0).get("licenseNo"));
        JSONObject tempJSONObject = dataObject.getJSONObject("data").getJSONArray("batchVehicleLists").getJSONObject(0);
        System.out.println(tempJSONObject.has("licenseNo"));
    }

    /**************************/
    /*** 遍历JSON 数组——>对象  ***/
    /**************************/
    @Test
    public void f6(){
        String[] sArray = {"vinNo","licenseNo","vehicleID"};
        String s = "[{\"vinNo\":\"ReaderTest\",\"licenseNo\":\"ReaderTest\",\"vehicleID\":\"ReaderTest\"},{\"vinNo\":\"VIN20201030102403\",\"licenseNo\":\"2\",\"vehicleID\":\"2\"}]";
        JSONArray jsonArray = new JSONArray(s);


        System.out.println(jsonArray.length());

        for(int i = 0; i < jsonArray.length(); i++){
            for(int j = 0; j < sArray.length; j++) {
                System.out.println(sArray[j] + " : " + jsonArray.getJSONObject(i).get(sArray[j]));
            }
        }
    }


    /**********************/
    /*** 判断字段是否存在 ****/
    /**********************/
    @Test
    public void f8(){
        JSONStringVO jsonStringVO = new JSONStringVO();
        String s = jsonStringVO.jsonString6;
        JSONObject jsonObject = new JSONObject(s);
//        System.out.println(jsonObject.toString());
//        JSONObject bodyObject = jsonObject.getJSONObject("data").getJSONObject("body");
//        System.out.println(bodyObject.toString());
//        String exchangeNo = bodyObject.get("exchangeNo") + "";
//        System.out.println(exchangeNo);

//        JSONArray exchangeDataJSONArray = bodyObject.getJSONArray("exchangeData");
//        System.out.println(exchangeDataJSONArray.getJSONObject(0).get("certiNo"));
        System.out.println(jsonObject.has("statuss"));
        System.out.println(jsonObject.getJSONObject("data").getJSONObject("body").has("exchangeData"));
//        String temp = jsonObject.get("status").toString();
//        System.out.println(temp);
//        System.out.println(jsonObject.getInt("status"));
//        System.out.println(jsonObject.getString("statusText"));

//        String temp  = "投保单 TDAA202033015000013740已生成保单，不能删除";
//        System.out.println(temp.contains("已生成保单，不能删除"));
    }

    /**********************/
    /***  JSON中添加字段  ***/
    /**********************/

    @Test
    public void f7(){
        String s = "{\"actualValue\":323800.00,\"applyNo\":\"FDAA202033015000035930\",\"brandName\":\"别克SGM7363AT轿车\",\"carInsuredRelation\":\"\",\"carKindCode\":\"A01\",\"carLotEquQuality\":1875.00,\"carcustType\":\"02\",\"clauseType\":\"F42\",\"countryNature\":\"03\",\"currency\":\"CNY\",\"energyType\":\"D1\",\"engineNo\":\"131312313132131312312313123123\",\"enrollDate\":\"2020-09-01\",\"exhaustScale\":0.0000,\"frameNo\":\"DSADSADSXADASDASD\",\"hkFlag\":\"0\",\"isEnergyCar\":\"0\",\"isRemote\":\"0\",\"itemNo\":ReaderTest,\"licenseColorCode\":\"01\",\"licenseFlag\":\"0\",\"licenseNo\":\"123123\",\"licenseType\":\"02\",\"loanVehicleFlag\":\"0\",\"modelCode\":\"BKAABD0002\",\"monopolyCode\":\"\",\"monopolyFlag\":\"0\",\"newCarCustType\":\"11\",\"newCarFlag\":\"ReaderTest\",\"noDamageYears\":\"0\",\"nonLocalFlag\":\"\",\"pKey\":\"FDAA202033015000035930\",\"purchasePrice\":323800.00,\"riskCode\":\"DAA\",\"runAreaCode\":\"11\",\"runAreaName\":\"11\",\"runMiles\":0,\"seatCount\":5,\"tonCount\":0.000,\"transferVehicleFlag\":\"0\",\"useNatureCode\":\"211\",\"useYears\":0,\"vinNo\":\"DSADSADSXADASDASD\",\"vehicleID\":\"\",\"prpCcarModel\":{\"airbagnums\":6,\"applyNo\":\"FDAA202033015000035930\",\"brandId\":\"BKA\",\"brandIdNew\":\"\",\"brandName\":\"别克\",\"brandNameNew\":\"\",\"dirrisk\":\"正常车型\",\"enginePower\":\"187\",\"engineType\":\"LY7\",\"fDeleted\":\"\",\"familyId\":\"BKAAB\",\"familyIdNew\":\"\",\"familyName\":\"林荫大道\",\"familyNameNew\":\"\",\"groupid\":\"BKA1AB01\",\"groupname\":\"林荫大道(07/04-14)\",\"isabs\":\"ReaderTest\",\"isalarm\":\"ReaderTest\",\"itemNo\":ReaderTest,\"pKey\":\"FDAA202033015000035930\",\"powerTypeCode\":\"D1\",\"priceP\":0,\"pricePr\":0,\"priceT\":0,\"priceTr\":0,\"remark\":\"手自一体 精英版 国Ⅲ\",\"riskCode\":\"DAA\",\"searchCode\":\"BK-SGM7363AT\",\"trackfront\":\"1602\",\"trackrear\":\"1620\",\"transMissionType\":\"手自一体\",\"vehicleExhaust\":3.565,\"vehicleFgwCode\":\"SGM7363AT\",\"vehicleId\":\"BKAABD0002\",\"vehicleMaker\":\"上海通用汽车有限公司\",\"vehicleMakerid\":\"\",\"vehicleName\":\"别克SGM7363AT轿车\",\"vehiclePrice\":351475.00,\"vehicleQuality\":ReaderTest.875,\"vehicleSize\":\"5175*1899*1480\",\"vehicleType\":\"2\",\"vehicleYear\":\"2007\",\"vehicleclass\":\"轿车类\",\"vehicleclasspicc\":\"A01\",\"vehicleidiac\":\"BTYPLZUC0007\",\"wheelbase\":\"3009\"},\"prpCitemCarExt\":{\"applyNo\":\"FDAA202033015000035930\",\"itemNo\":ReaderTest,\"lastDamagedBI\":0,\"lastDamagedCI\":0,\"noDamYearsBI\":0,\"noDamYearsCI\":0,\"pKey\":\"FDAA202033015000035930\",\"riskCode\":\"DAA\",\"thisDamagedBI\":0,\"thisDamagedCI\":0},\"prpCcarDevices\":[]} ";
        JSONObject jsonObject = new JSONObject(s);
        jsonObject.put("projectCode","11111111111111111");
        System.out.println(jsonObject.toString());
        String[] ss = {};
        s.length();
    }

    //JSON 添加Map.put("list",list2.add(map1,map2))
    @Test
    public void f3(){
        List list = new ArrayList();

        Map map = new HashMap();
        List list2 = new ArrayList();

        Map map1 = new HashMap();
        Map map2 = new HashMap();
        String[] strings = {"name"};

        map1.put(strings[0],"map1");
        map2.put("name","map2");
        list2.add(map1);
        list2.add(map2);

        map.put("age",1);
        map.put("name","zhangsan");
        map.put("list",list2);
        list.add(map);

//        JSONArray jsonObject = new JSONArray(list);
//        System.out.println(new JSONArray(list).toString());

        System.out.println(new JSONObject(map).toString());
    }

    @Test
    public void fffff() throws ParseException {

//        Map map=new HashMap();
//        JSONObject jsonObject = new JSONObject(map);
//        System.out.println(jsonObject.toString());
//        List list = new ArrayList();
//        JSONArray jsonArray = new JSONArray("[]");
//        System.out.println(jsonArray.toString());
        Map<String,String> map = new HashMap<>();
        map.put("fa",null);
        JSONObject jsonObject = new JSONObject(map);
        System.out.println(jsonObject.toString());

    }

    @Test
    public void fv(){
        String s = "{\"status\":2,\"statusText\":\"返回成功\",\"data\":{\"classList\":[{\"className\":\"class one\",\"classNum\":40,\"kids\":[{\"name\":\"xiaoming\",\"age\":6},{\"name\":\"xiaohong\",\"age\":7},{\"name\":\"xiaowang\",\"age\":7}]},{\"className\":\"class two\",\"classNum\":39}],\"xiaozhang\":\"zhangsan\"}}";
        JSONObject jsonObject = new JSONObject(s);
        System.out.println(jsonObject.getString("status"));
    }
}
